#include "thread.hpp"
#include <arpa/inet.h>
#include <cstdint>
#include <cstring>
#include<iostream>
#include <memory>
#include <netinet/in.h>
#include<string>
#include <sys/socket.h>
#include <unistd.h>

uint16_t serverport=0;
std::string serverip;

static void* udpSend2(void* args)
{
    int sock=*(int*)((ThreadData*)args)->_args;
    std::string name=((ThreadData*)args)->_name;
    std::string message;
    struct sockaddr_in server;
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(serverport);
    server.sin_addr.s_addr=inet_addr(serverip.c_str());
    while(true){
        std::cerr<<"请输入您的信息"<<std::endl;
        std::string message;
        std::getline(std::cin,message);
        sendto(sock,message.c_str(),message.size(),0,
        (struct sockaddr*)&server,sizeof(server));
    }
    return nullptr;
}
//这个接口的意义就是将消息从显示屏里面读取到服务器
static void* udpSend(void* args)
{
    int sock=*(int*)((ThreadData*)args)->_args;
    std::string name=((ThreadData*)args)->_name;
    std::string message;
    struct sockaddr_in server;
    memset(&server,0,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(serverport);
    server.sin_addr.s_addr=inet_addr(serverip.c_str());
    while(true)
    {
        std::cerr<<"请输入您的信息"<<std::endl;
        std::getline(std::cin,message);
        if(message=="quit"){
            break;
        }
        sendto(sock,message.c_str(),message.size(),0,
        (struct sockaddr*)&server,sizeof(server));
    }
    return nullptr;
}

static void* udpRecv(void* args)
{
    int sock=*(int*)((ThreadData*)args)->_args;
    std::string name=((ThreadData*)args)->_name;
    char buffer[1024];
    while(true)
    {
        struct sockaddr_in server;
        socklen_t len=sizeof(server);
        bzero(&server,sizeof(server));
        ssize_t s=recvfrom(sock,buffer,sizeof(buffer)-1,0,
        (struct sockaddr*)&server,&len);
        if(s > 0)
        {
            buffer[s]=0;
            //将我们收到的数据显示出来
            std::cout<<buffer<<std::endl;
        }
    }
}
static void* udpRecv3(void* args)
{
    int sock=*(int*)((ThreadData*)args)->_args;
    std::string name=((ThreadData*)args)->_name;
    char buffer[1024];
    while(true)
    {
        struct sockaddr_in server;
        bzero(&server,sizeof(server));
        socklen_t len=sizeof(server);
        ssize_t s=recvfrom(sock,buffer,sizeof(buffer),0,
        (struct sockaddr*)&server,&len);
        if(s > 0)
        {
            buffer[s]=0;
            std::cout<<buffer<<std::endl;
        }
    }
}
int main(int argc,const char* argv[])
{
    if(argc!=2){
        exit(1);
    }
    int sock=socket(AF_INET,SOCK_DGRAM,0);
    if(sock < 0)
    {
        std::cout<<"socket() failed"<<std::endl;
        exit(2);
    }
    serverport=atoi(argv[2]);
    serverip=argv[1];
    std::unique_ptr<Thread>sender(new Thread(1,udpSend,(void*)&sock));
    std::unique_ptr<Thread>recver(new Thread(2,udpRecv,(void*)&sock));
    sender->start();
    recver->start();
    sender->join();
    recver->join();
    close(sock);
    return 0;
}